module.exports = app => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;
  const ObjectId = Schema.Types.ObjectId;
  /**
   * @ menu data schema
   */
  let MenuSchema = new Schema({
    name: String,
    path: String,
    component: String,
    sort: { type: Number, default: 1},
    title: String,
    icon: { type: String, default: '' },
    show: { type: Number, default: 1 },
    type: Number,   // 0-dir/1-menu/2-btn
    permi: { type: String, default: '' },   // permission
    pid: { type: String, default: '' },  // 父级ID
    date: { type: Date, default: Date.now }
  }, {usePushEach: true});

  MenuSchema.statics = {
    add(data) {
      return data.save();
    },
    update(mid, params) {
      return this.findByIdAndUpdate({_id: mid}, {$set: params}, {new: true});
    },
    del(mid) {
      return this.remove({ _id: mid }).exec();
    },
    findAll: function(cb) {
      return this.find({})
        .populate('menu', {"_id":0,"name":1})
        .exec(cb);
    },
    findByIds(ids, cb) {
      return this.find({ _id: {$in: ids} }).select('permi');
    },
    findById: function(mid, cb) {
      return this.findOne({ _id: mid }).exec(cb);
    }
  };

  return mongoose.model('Menu', MenuSchema);
}